
package com.funo.increment;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import java.io.File;

/**
 * 下载文件过程会发送广播。<br>
 * Intent intent = new Intent(MyContant.DOWN_PROCESS_ACTION);
 * intent.putExtra(MyContant.DOWN_PROCESS, process);//百分比进度条
 * intent.putExtra(MyContant.DOWN_PROCESS_TOTAL, all + "M");//这个是总大小
 */
public class DownService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private final static String TAG = "increment_DownService";

    private PowerLock powerLock;

    private DownThread downThread;

    @Override
    public void onCreate() {
        super.onCreate();
        powerLock = new PowerLock(this, TAG);
        powerLock.acquireWakeLock();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);

        if (intent != null) {
            String uri = intent.getStringExtra(MyContant.URI_KEY);
            String packageName = intent.getStringExtra(MyContant.PACKAGENAME_KEY);
            String fileName = intent.getStringExtra(MyContant.FILENAME_KEY);
            if (downThread == null) {
                downThread = new DownThread(this, uri, packageName, fileName);
                downThread.start();
            }
        }

        // 服务被kill不用许被重启。
        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        Log.i(TAG, "increment_DownService destroy");
        if (powerLock != null) {
            powerLock.releaseWakeLock();
            powerLock = null;
        }
        try {
            if (downThread != null && downThread.isAlive()) {
                downThread.interrupt();
                downThread = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        super.onDestroy();
    }

    /**
     * 业务合并线程。
     * 
     * @author suchangjun
     */
    class DownThread extends Thread {

        private Context context;

        private String uri;

        private String packageName;

        private String wantDownFileName;

        public DownThread(Context context, String uri, String packageName, String wantDownFileName) {
            this.context = context;
            this.packageName = packageName;
            this.wantDownFileName = wantDownFileName;
            this.uri = uri;
        }

        @Override
        public void run() {

            try {
                File file = MyUtil.beginUpdate(context, uri, packageName, wantDownFileName);
                if (file != null) {
                    MyUtil.installApk(file.getPath(), context);
                } else {
                    boolean isApk = wantDownFileName.toLowerCase().endsWith("apk");
                    MyUtil.sendDowning(context, "0%", 0, false, isApk);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                stopSelf();
            }

        }
    }
}
